const express = require('express')
const router = express.Router()
const Good = require('../../model/good');
//获取所有商品
router.get('/goods', (req, res, next) => {
    let type = req.query.type
    if (type == 'new') {
        Good.find().then(data => {
            let goods = []
            data.forEach(e => {
                let good = {
                    gid: e._id,
                    img: e.img[0],
                    goodname: e.goodname,
                    price: e.price,
                    uid: e.user,
                    click: e.click
                }
                goods.push(good)
            })
            res.send(goods)
        })
    } else {
        Good.find({
            type: type
        }).then(data => {
            let goods = []
            data.forEach(e => {
                let good = {
                    gid: e._id,
                    img: e.img[0],
                    goodname: e.goodname,
                    price: e.price,
                    uid: e.user,
                    click: e.click
                }
                goods.push(good)
            })
            res.send(goods)
        })
    }
});
//根据用户点击获取具体商品
router.get('/detail', (req, res, next) => {
    let gid = req.query.gid
    console.log(gid)
    Good.findById(gid).populate('user').then(data => {
        let good = {
            goodname: data.goodname,
            banners: data.img,
            goodinfo: data.goodinfo,
            price: data.price,
            address: data.address,
            type: data.type,
            collections: data.collections,
            user: {
                nickname: data.user.nickname,
                avatar: data.user.avatar,
                uid: data.user._id,
                username: data.user.username
            }
        }
        let count = (data.click + 1) * 1;
        Good.findByIdAndUpdate(gid, { click: count }, (err, ret) => {
            if (err) {
                console.log(err)
            }
            return console.log('商品点击量加1')
        })
        res.send(good)
    })
});
//发布商品
router.post('/submit', (req, res, next) => {
    let good = new Good({
        goodname: req.body.goodName,
        goodinfo: req.body.goodInfo,
        price: req.body.price,
        address: req.body.address,
        type: req.body.type,
        user: req.body._id,
        img: req.body.img
    })
    good.save()
    res.send({
        msg: '发布成功'
    })
});
//收藏
router.post('/collect', (req, res, next) => {
    console.log(req.body)
    let uid = req.body.uid;
    let gid = req.body.gid;
    Good.findById(gid).then(result => {
        console.log(result)
        let newArr = result.collections
        let d = newArr.indexOf(uid)
        if (d == -1) {
            newArr.push(uid)
            console.log('用户收藏')
        } else {
            newArr.splice(d, 1)
            console.log('用户取消收藏')
        }
        Good.findByIdAndUpdate(gid, { collections: newArr }, (err, ret) => {
            if (err) {
                console.log(err)
            } else {
                console.log('收藏更新成功')
                res.send('1')
            }
        })
    })
});
//获取用户收藏的商品
router.get('/collections', async(req, res, next) => {
    let uid = req.query.uid;
    console.log(uid)
    let result = await Good.find()
    let collections = []
    result.forEach(e => {
        let d = e.collections.indexOf(uid)
        if (d != -1) {
            collections.push(e)
        }
    });
    // console.log(result)
    res.send(collections)
})
module.exports = router